﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class search_round_all : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2400)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                if (day2.Length == 1)
                {
                    day2 = "0" + day2;
                }
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            DateTime datte = DateTime.Now;
            string sDate = datte.ToShortDateString();
            string check_date = ConvestToDate_check(TextBox3.Text);
            DateTime ch_1 = Convert.ToDateTime(sDate);
            DateTime ch_2 = Convert.ToDateTime(check_date);
            if (check_date == "1111/1/1")
            {
                check_date = "";
            }
            string ss = TextBox4.Text;
            string type_bus = DropDownList2.SelectedValue;
            if (type_bus == "0")
            {
                type_bus = "";
            }
            string Around_on = DropDownList3.SelectedValue;
            if (Around_on == "0")
            {
                Around_on = "";
            }
            var q = from p in db.Table_Arounds
                    join c in db.Table_Routes on p.Route_id equals c.Route_id
                    where p.Around_no.Contains(Around_on) &
                            p.Table_Route.Route_source.Contains(TextBox1.Text) &
                            p.Table_Route.Route_destination.Contains(TextBox2.Text) &
                            p.Table_Time.Time_time.Contains(ss) &
                            (p.Around_date_chk.ToString().Contains(check_date.ToString()) & p.Around_date >= DateTime.Now) &
                            p.Type_Bus_id.ToString().Contains(type_bus)
                    orderby p.Around_no
                    select new
                    {
                        รหัสรอบเดินทาง = p.Around_no,
                        รหัสเส้นทาง = p.Route_id,
                        ชื่อต้นทาง = p.Table_Route.Route_source,
                        ชื่อปลายทาง = p.Table_Route.Route_destination,
                        วันที่เดินทาง = p.Around_date,
                        //เวลาเดินทาง = p.Table_Time.Time_time,
                        ประเภทรถ = p.Table_Type_Bus.Type_Bus_name,
                        จำนวนที่นั่ง = p.Table_Type_Bus.Type_Bus_number,
                        //จำนวนที่นั่ง = p.Table_Type_Bus.Type_Bus_number,
                        จำหน่ายไป = (from a in db.Table_Sales
                                     join b in db.Table_SaleDetails
                                     on a.Sale_id equals b.Sale_id
                                     join d in db.Table_Arounds
                                       on a.Around_id equals d.Around_id
                                     where d.Around_date_chk.ToString().Contains(check_date.ToString()) &
                                            d.Table_Time.Time_time.Contains(ss) &
                                            d.Around_no.Contains(Around_on) &
                                            (a.SSID == 1 || a.SSID == 2) &
                                            d.Around_id == p.Around_id
                                     select a).Count()

                    };
            GridView1.DataSource = q.ToList();
            GridView1.DataBind();         
        }
    }
}